package com.xhdx;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class TestJsonCarBin {
    public static void main(String args[]) {
        int x = 1;
        /* 写入Txt文件 */
        File writename = new File("d:\\output.txt"); // 相对路径，如果没有则要建立一个新的output.txt文件
        BufferedWriter out = null;
        try {
            writename.createNewFile(); // 创建新文件
            out = new BufferedWriter(new FileWriter(writename));
            InputStream inputStream = new FileInputStream("d:\\bankInfo.json");
            StringBuilder sb = new StringBuilder();
            String line;

            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            String str = sb.toString();
            JSONObject jsonObject = JSON.parseObject(str);
            Set<String> keys = jsonObject.keySet();
            System.out.println(keys);

            for (String key : keys) {
                JSONObject chObj = (JSONObject) jsonObject.get(key);
                String bankName = chObj.getString("name");
                String payBankNo = chObj.getString("id");
                String type = chObj.getString("type");
                out.write(
                        "INSERT INTO ptp_bank_card_info(ID_,PAY_BANK_NO_,BANK_NAME_,CARD_BIN_,Card_Type_,BANK_CODE_) VALUES ('"
                                + (x++) + "', '" + payBankNo + "', '" + bankName + "', '" + key + "', '" + type
                                + "',null);\r\n"); // \r\n即为换行
            }
        } catch (Exception e) {

        } finally {
            try {
                out.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // 把缓存区内容压入文件
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // 最后记得关闭文件
        }
        System.out.println("完毕" + x);
    }
}
